package cn.itrip.auth.controller;

import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/api")
public class UserController {
    @Resource
    private UserService userService;


    //邮箱注册
    @RequestMapping(value = "/doregister",method = RequestMethod.POST,produces = "application/json")
    public @ResponseBody Dto doRegister(@RequestBody ItripUserVO vo){
        //邮箱验证
        if(!this.validEmail(vo.getUserCode()))
            return DtoUtil.returnFail("请使用正确的邮箱地址", ErrorCode.AUTH_ILLEGAL_USERCODE);
        //2.调用业务层createUser
        ItripUser user=new ItripUser();
        user.setUserCode(vo.getUserCode());
        user.setUserName(vo.getUserName());
        //user.setUserPassword(vo.getUserPassword());
        try {
            if(null!=userService.findUserByUserCode(user.getUserCode()))
                return  DtoUtil.returnFail("用户已存在",ErrorCode.AUTH_USER_ALREADY_EXISTS);
            else {
                user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
                userService.itriptxCreateUser(user);
                return DtoUtil.returnSuccess();//表示添加成功
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
        }
        //return DtoUtil.returnFail("注册失败",ErrorCode.AUTH_UNKNOWN);
    }


    //邮箱激活
    @ResponseBody
    @RequestMapping(value = "/activate",method = RequestMethod.PUT,produces = "application/json")
    public Dto activate(@RequestParam String user, @RequestParam String code){
        try {
            if(userService.activate(user,code))
                return DtoUtil.returnSuccess("激活成功");
            else
                return DtoUtil.returnSuccess("激活失败");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("激活失败",ErrorCode.AUTH_UNKNOWN);
        }
    }
    //手机注册
    @RequestMapping(value = "/registerbyphone",method = RequestMethod.POST,produces = "application/json")
    public @ResponseBody Dto registerByPhone(@RequestBody ItripUserVO vo){
        //1.手机号验证
        if(!this.validPhone(vo.getUserCode())){
            return DtoUtil.returnFail("请使用正确的手机号码",ErrorCode.AUTH_ILLEGAL_USERCODE);
        }
        //2.调用Service
        ItripUser user=new ItripUser();
        user.setUserCode(vo.getUserCode());
        user.setUserName(vo.getUserName());
        //user.setUserPassword(vo.getUserPassword());
        try {
            if(null!=userService.findUserByUserCode(user.getUserCode()))
                return  DtoUtil.returnFail("用户已存在",ErrorCode.AUTH_USER_ALREADY_EXISTS);
            else {
                user.setUserPassword(MD5.getMd5(vo.getUserPassword(),32));
                userService.itriptxCreateUserByphone(user);
                return DtoUtil.returnSuccess();//表示添加成功
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
        }
    }
    //手机短信码验证
    @ResponseBody
    @RequestMapping(value = "/validatephone",method = RequestMethod.PUT,produces = "application/json")
    public Dto validatePhone(@RequestParam String user, @RequestParam String code){
        try {
            if(userService.validatePhone(user,code))
                return DtoUtil.returnSuccess("验证成功");
            else
                return DtoUtil.returnSuccess("验证失败");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("验证失败",ErrorCode.AUTH_UNKNOWN);
        }
    }
    //邮箱验证
    private boolean validEmail(String email){
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        return Pattern.compile(regex).matcher(email).find();
    }
    //手机验证
    private boolean validPhone(String phone){
        String regex="^1[3578]{1}\\d{9}$";
        return Pattern.compile(regex).matcher(phone).find();
    }

    @ResponseBody
    @RequestMapping(value = "/ckusr",method = RequestMethod.GET,produces = "application/json")
    private Dto ckusr(@RequestParam String name){
        try {
            ItripUser userByUserCode = userService.findUserByUserCode(name);
            if(EmptyUtils.isEmpty(userByUserCode)){
                return DtoUtil.returnSuccess("邮箱未注册");
            }else {
                return DtoUtil.returnFail("邮箱已注册",ErrorCode.AUTH_USER_ALREADY_EXISTS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("邮箱已注册",ErrorCode.AUTH_USER_ALREADY_EXISTS);
    }
}
